package ch.uzh.ifi.seal.ssqa.ui.views;


import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.zest.core.viewers.AbstractZoomableViewer;
import org.eclipse.zest.core.viewers.GraphViewer;
import org.eclipse.zest.core.viewers.IZoomableWorkbenchPart;
import org.eclipse.zest.core.viewers.ZoomContributionViewItem;
import org.eclipse.zest.core.widgets.ZestStyles;
import org.eclipse.zest.layouts.LayoutAlgorithm;
import org.eclipse.zest.layouts.LayoutStyles;
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;

import ch.uzh.ifi.seal.ssqa.model.DiagramComponent;
import ch.uzh.ifi.seal.ssqa.model.Source;
import ch.uzh.ifi.seal.ssqa.storage.IStorage;
import ch.uzh.ifi.seal.ssqa.storage.StorageResult;
import ch.uzh.ifi.seal.ssqa.storage.TeneoHibernateStorage;
import ch.uzh.ifi.seal.ssqa.ui.providers.FileModelContentProvider;
import ch.uzh.ifi.seal.ssqa.ui.providers.SourceFigureProvider;

public class FileModelView extends ViewPart implements IZoomableWorkbenchPart {
	
	public static final String ID = "ch.uzh.ifi.seal.ssqa.ui.views.FileModelView";
	private GraphViewer viewer;

	@Override
	public void createPartControl(org.eclipse.swt.widgets.Composite comp) {
		this.viewer = new GraphViewer(comp, SWT.BORDER);
		this.viewer.setContentProvider(new FileModelContentProvider());
		viewer.setLabelProvider(new SourceFigureProvider());
		viewer.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
		this.fillToolBar();
	}

	@Override
	public void setFocus() {
	}
	
	public void openModelView(DiagramComponent component){
		((SourceFigureProvider)this.viewer.getLabelProvider()).setMapping(component.getIdentifier());
		IStorage storage = new TeneoHibernateStorage();
		StorageResult result = storage.executeListQuery("from Source where headerfile = 1 and diagrammapping = '"+component.getIdentifier()+"'");
		List<Source> sources = (List<Source>) result.getListResult();
		System.out.println("Size: "+sources.size());
		viewer.setInput(sources);
		
		LayoutAlgorithm layout = setLayout();
		viewer.setLayoutAlgorithm(layout, true);
	}
	
	private LayoutAlgorithm setLayout() {
		LayoutAlgorithm layout;
//		layout = new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
		layout = new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
//		layout = new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
//		layout = new HorizontalTreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
//		layout = new RadialLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
		return layout;

	}
	
	private void fillToolBar() {
		ZoomContributionViewItem toolbarZoomContributionViewItem = new ZoomContributionViewItem(this);
		IActionBars bars = getViewSite().getActionBars();
		bars.getMenuManager().add(toolbarZoomContributionViewItem);

	}

	@Override
	public AbstractZoomableViewer getZoomableViewer() {
		return viewer;
	}
}
